package com.ruoyi.jwxt.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.RepairStatusConstants;
import com.ruoyi.common.constant.TeacherStatusConstants;
import com.ruoyi.jwxt.domain.PO.JwxtLabMaintenance;
import com.ruoyi.jwxt.domain.PO.JwxtTeacherApplication;
import com.ruoyi.jwxt.mapper.JwxtLabMaintenanceMapper;
import com.ruoyi.jwxt.mapper.JwxtTeacherApplicationMapper;
import com.ruoyi.jwxt.service.IJwxtLabSchedulingService;
import com.ruoyi.jwxt.service.IJwxtLabService;
import com.ruoyi.jwxt.service.IJwxtTeacherApplicationService;

/**
 * 教师实验课申请登记Service业务层处理
 */
@Service
public class JwxtTeacherApplicationServiceImpl implements IJwxtTeacherApplicationService {
    @Autowired
    private JwxtTeacherApplicationMapper jwxtTeacherExperimentApplicationMapper;

    @Autowired
    private JwxtLabMaintenanceMapper jwxtExperimentEquipmentMaintenanceMapper;

    @Autowired
    private IJwxtLabSchedulingService jwxtLabSchedulingService;

    @Autowired
    private IJwxtLabService jwxtLabService;

    /**
     * 新增教师实验课申请登记
     */
    @Override
    public int insert(JwxtTeacherApplication jwxtTeacherApplication) {
        if (jwxtLabSchedulingService.getFreeLabs(jwxtTeacherApplication).size() > 0) {
            jwxtTeacherApplication.setApplicationStatus(TeacherStatusConstants.PENDING);
            jwxtTeacherApplication.setCreateTime(new Date());
            return jwxtTeacherExperimentApplicationMapper.insert(jwxtTeacherApplication);
        }
        return 0;
    }

    /**
     * 批量删除教师实验课申请登记
     */
    @Override
    public int delete(Long[] applicationIds) {
        return jwxtTeacherExperimentApplicationMapper.delete(applicationIds);
    }

    /**
     * 删除教师实验课申请登记
     */
    @Override
    public int delete(Long applicationId) {
        Long[] applicationIds = { applicationId };
        return jwxtTeacherExperimentApplicationMapper.delete(applicationIds);
    }

    /**
     * 查询教师实验课申请登记列表
     */
    @Override
    public List<JwxtTeacherApplication> select(JwxtTeacherApplication jwxtTeacherApplication) {
        return jwxtTeacherExperimentApplicationMapper.select(jwxtTeacherApplication);
    }

    /**
     * 查询教师实验课申请登记
     */
    @Override
    public JwxtTeacherApplication select(Long applicationId) {
        JwxtTeacherApplication jwxtTeacherApplication = new JwxtTeacherApplication();
        jwxtTeacherApplication.setApplicationId(applicationId);
        return select(jwxtTeacherApplication).get(0);
    }

    /**
     * 修改教师实验课申请登记
     */
    @Override
    public int update(JwxtTeacherApplication jwxtTeacherApplication) {
        if (jwxtTeacherApplication.getApplicationStatus().equals(TeacherStatusConstants.PENDING)) {
            return jwxtTeacherExperimentApplicationMapper.update(jwxtTeacherApplication);
        }
        return 0;
    }

    /**
     * 新增报修，填写新的报修，新报修的状态为“未维修”
     */
    @Override
    public int insertApplication(JwxtLabMaintenance jwxtExperimentEquipmentMaintenance) {
        jwxtExperimentEquipmentMaintenance.setRepairStatus(RepairStatusConstants.NOT_REPAIRED);
        jwxtExperimentEquipmentMaintenance.setExperimenterId(jwxtLabService.select(jwxtExperimentEquipmentMaintenance.getLabId()).getExperimenterId());
        return jwxtExperimentEquipmentMaintenanceMapper.insert(jwxtExperimentEquipmentMaintenance);

    }

    /**
     * 批量删除教师报修
     */
    @Override
    public int deleteApplication(Long[] applicationIds) {
        return jwxtExperimentEquipmentMaintenanceMapper.delete(applicationIds);
    }

    /**
     * 删除教师单个报修
     */
    @Override
    public int deleteApplication(Long applicationId) {
        Long[] applicationIds = { applicationId };
        return jwxtExperimentEquipmentMaintenanceMapper.delete(applicationIds);
    }

    /**
     * 列出教师的所有设备报修(报修状态有：已维修、未维修、维修中)
     */
    @Override
    public List<JwxtLabMaintenance> selectApplication(Long teacherId) {
        JwxtLabMaintenance jwxtExperimentEquipmentMaintenance = new JwxtLabMaintenance();
        jwxtExperimentEquipmentMaintenance.setTeacherId(teacherId);
        return jwxtExperimentEquipmentMaintenanceMapper.select(jwxtExperimentEquipmentMaintenance);
    }

    /**
     * 修改教师的设备报修
     */
    public int updateApplication(JwxtLabMaintenance jwxtExperimentEquipmentMaintenance) {
        return jwxtExperimentEquipmentMaintenanceMapper.update(jwxtExperimentEquipmentMaintenance);
    }

}
